package com.example.demo.zaqizabatest;

import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

/**
 * 使用treeMap需要注意，Comparator
 * @ClassName TreeMapDemo
 * @Author lys4134
 * @Date 2022/02/15 10:09:28
 * @Description TtreeMapDemo
 * @Version 1.0
 **/
public class TreeMapDemo {
    public static void main(String[] args) {
        Map<Student, String> map = new TreeMap<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1 == null || o2 == null) {
                    return 0;
                }
                return o1.getName().compareTo(o2.getName());
            }
        });
        map.put(new Student("1", "221", 101), "100");
        map.put(new Student("2", "22", 102), "100");
        map.put(new Student("3", "22", 103), "100");
        System.out.println(map);
    }
}
class Student {
    private String no;
    private String name;
    private int age;

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Student(String no, String name, int age) {
        this.no = no;
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(no, student.no) &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(no, name, age);
    }

    @Override
    public String toString() {
        return "{" +
                "no='" + no + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}